package com.czh.filter;

import com.czh.pojo.User;
import com.czh.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        User user = (User) httpServletRequest.getSession().getAttribute(Constants.USER_SESSION);
        if(user == null){
            httpServletResponse.sendRedirect("/s/error.jsp");
        }else {chain.doFilter(httpServletRequest,httpServletResponse);
        }
    }

    @Override
    public void destroy() {
    }
}
